<template>
  <div class="goodsinfo-container">
    <!-- 轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <!-- <swiper :isfull="false"></swiper> -->
          <swiper></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <!-- 小球动画 -->
      <div class="ball" v-show="flag" ref="ball"></div>
    </transition>

    <div class="mui-card">
      <div class="mui-card-header">商品的名称标题</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:
            <del>$888</del>&nbsp;&nbsp;销售价:
            <span class="sjia">${{msg}}</span>
          </p>
          <p>
            购买数量:
            <number @getcount="getSelectedCount" :max="99"></number>
            <!-- :max="goodsinfolist.number" 父向子传递数据  库存最大值 -->
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="primary" size="small" @click="addshop">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数区</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:0001</p>
          <p>库存情况:库存充足,尽情买吧!</p>
          <p>上架时间:{{new Date() | dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="primary" size="large" plain @click="gocomment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
//导入轮播图组件
import swiper from '../subcompontens/lubotu.vue';
//导入数字选择框
import number from '../subcompontens/goods_nubbox.vue';
export default {
  data () {
    return {
      id: this.$route.params.id,  //将路由参数上
      lubotu: [],
      msg: '666',
      goodsinfolist: [],
      flag: false,  //控制小球显示和隐藏
      selectedCount: 1 // 保存用户选中的商品数量， 默认，认为用户买1个
    }
  },
  created () {
    this.lubotulist();
    this.getGoodsinfo();
  },
  methods: {
    goDesc (id) {
      //使用编程式导航 跳转到图文介绍
      this.$router.push({ name: "godesc", params: { id } });

    },
    gocomment (id) {
      //使用编程式导航 跳转到商品评论
      this.$router.push({ name: "gocomment", params: { id } });
    },
    lubotulist () {
      this.$http.get("api/getyhumimages/" + this.id).then(result => {
        result.body.message.forEach(item => {
          item.img = item.src;
        })
        if (result.body.status == 0) {
          this.lubotu = result.body.message;
        }
      })
    },
    getGoodsinfo () {
      this.$http.get('api/goods/getinfo' + this.id).then(result => {
        if (result.body.status == 0) {
          this.goodsinfolist = result.body.message[0];
        }
      })
    },
    addshop () {
      this.flag = !this.flag;
      //{id:商品的id,count:要购买的数量,price:商品的单价,selected:false}
      var goodsinfo = {
        id: "23",
        count: this.selectedCount,
        price: 666,
        selected: true
      };
      // 调用 store 中的 mutations 来将商品加入购物车
      this.$store.commit("addToCar", goodsinfo);
    },
    beforeEnter (el) {
      el.style.transform = "translate(0, 0)";
    },
    enter (el, done) {
      el.offsetWidth;
      // 获取小球的 在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取 徽标 在页面中的位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();
      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter (el) {
      this.flag = !this.flag;
    },
    getSelectedCount (count) {
      // 当子组件把 选中的数量传递给父组件的时候，把选中的值保存到 data 上
      this.selectedCount = count;
      console.log("父组件拿到的数量值为： " + this.selectedCount);
    }
  },
  components: {
    swiper,
    number
  }
} 
</script>
<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  .sjia {
    color: red;
    font-size: 16px;
  }
}
.mui-card-footer {
  display: block;
  button {
    margin: 15px 0;
  }
}

.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  z-index: 99;
  top: 390px;
  left: 150px;
}
</style>